package com.ruoyi.common.config;

import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.androidpublisher.AndroidPublisher;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.security.GeneralSecurityException;

@Configuration
@ConfigurationProperties(prefix = "google.payment")
public class GooglePaymentConfig {
    private static final String APPLICATION_NAME = "OraMemo";
    private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();

    private String serviceAccountEmail;
    private String p12KeyFilePath;
    private String packageName;

    public String getServiceAccountEmail() {
        return serviceAccountEmail;
    }

    public void setServiceAccountEmail(String serviceAccountEmail) {
        this.serviceAccountEmail = serviceAccountEmail;
    }

    public String getP12KeyFilePath() {
        return p12KeyFilePath;
    }

    public void setP12KeyFilePath(String p12KeyFilePath) {
        this.p12KeyFilePath = p12KeyFilePath;
    }

    public String getPackageName() {
        return packageName;
    }

    public void setPackageName(String packageName) {
        this.packageName = packageName;
    }

    @Bean
    public AndroidPublisher androidPublisher() throws GeneralSecurityException, IOException {
        HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
//        GoogleCredential credential = new GoogleCredential.Builder()
//                .setTransport(httpTransport)
//                .setJsonFactory(JSON_FACTORY)
//                .setServiceAccountId(serviceAccountEmail)
//                .setServiceAccountScopes(Collections.singleton(AndroidPublisherScopes.ANDROIDPUBLISHER))
//                .setServiceAccountPrivateKeyFromP12File(new FileInputStream(p12KeyFilePath))
//                .build();
//        return new AndroidPublisher.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME).build();
        return new AndroidPublisher.Builder(httpTransport,JSON_FACTORY,null).setApplicationName(APPLICATION_NAME).build();
    }
}
